﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;

namespace UploadBlobToAzure
{
    public partial class Form1 : Form
    {
        TreeNode selectedNode;
        BlobRequestOptions options = new BlobRequestOptions();
        CloudPageBlob clpageBlob;
        string size;
 
        
        private void treeContainerList_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            
        }

        private void treeContainerList_MouseClick(object sender, MouseEventArgs e)
        {           
            
            selectedNode = treeContainerList.HitTest(e.Location).Node;
            string type = string.Empty;
            gridContainer.Rows.Clear();
            gridContainer.ColumnCount = 2;
            gridContainer.Columns[0].Name = "Name";
            gridContainer.Columns[1].Name = "Size";
            options.UseFlatBlobListing = true;
            blobList.Clear();
            blobDirList.Clear();

            //customMessageForm = new custom_messages();
                        
            if (e.Button == MouseButtons.Left)
            {
                if (selectedNode.Level == 1)
                {
                                   
                    container = blobClient.GetContainerReference(selectedNode.Text);                    
                    
                    pictureBoxGridView.Visible = true;
                    
                    try
                    {
                        foreach (var blobItems in container.ListBlobs())
                        {
                            if (blobItems.GetType() == typeof(CloudBlobDirectory))
                            {
                                blobDirList.Add(blobItems.Uri.AbsolutePath);
                            }
                        }
                        

                        foreach (CloudBlob blobItem in container.ListBlobs(options))
                        {
                            var absolutePath = blobItem.Uri.AbsolutePath;
                            blobList.Add(absolutePath);
                            cloudBlobListforSearch.Add(blobItem);
                        }
                        populateGridView(container);
                        manageBlobToolStripMenuItem.Enabled = true;
                        
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }                                      

                    txtSearch.Enabled = true;                   
                    
                }                
            }
        }

        public void populateGridView(CloudBlobContainer m_container)
        {
            
            options.UseFlatBlobListing = true;
                gridContainer.BeginInvoke(new Action(() =>
                {
                    foreach(var blobItem in container.ListBlobs(options))
                    {
                        clpageBlob = new CloudPageBlob((CloudBlob)blobItem);
                        var sizeLong = clpageBlob.Properties.Length;
                        if(sizeLong<1024)
                        {
                             size = (clpageBlob.Properties.Length).ToString()+" Bytes";
                        }
                        else
                        {
                             size = ((float)(clpageBlob.Properties.Length)/1024/1024).ToString()+" MB";
                        }
                        
                         var absolutePath = blobItem.Uri.AbsolutePath;
                        
                        gridContainer.Rows.Add(absolutePath,size);
                        //customMessageForm.Close();
                    }
                }
                ));
                pictureBoxGridView.Visible = false;                
                //changeLabelStatusForm("Population Done!!");                
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            gridContainer.Rows.Clear();
            gridContainer.ColumnCount = 2;
            gridContainer.Columns[0].Name = "Name";
            gridContainer.Columns[1].Name = "Size";
            BlobRequestOptions options = new BlobRequestOptions();
            options.UseFlatBlobListing = true;

            if (txtSearch.Text == string.Empty)
            {

                foreach (var blobItem in cloudBlobListforSearch)
                {
                    clpageBlob = new CloudPageBlob(blobItem);
                    var sizeLong = clpageBlob.Properties.Length;
                    if (sizeLong < 1024)
                    {
                        size = (clpageBlob.Properties.Length).ToString() + " Bytes";
                    }
                    else
                    {
                        size = ((float)(clpageBlob.Properties.Length) / 1024 / 1024).ToString() + " MB";
                    }
                    gridContainer.Rows.Add(blobItem.Attributes.Uri.AbsolutePath,size);
                }

            }
            else
            {
                foreach (var blobItem in cloudBlobListforSearch)
                {                    
                    if (blobItem.Attributes.Uri.AbsolutePath.ToUpperInvariant().Contains(txtSearch.Text.ToUpperInvariant()))
                    {
                        clpageBlob = new CloudPageBlob(blobItem);
                        var sizeLong = clpageBlob.Properties.Length;
                        if (sizeLong < 1024)
                        {
                            size = (clpageBlob.Properties.Length).ToString() + " Bytes";
                        }
                        else
                        {
                            size = ((float)(clpageBlob.Properties.Length) / 1024 / 1024).ToString() + " MB";
                        }
                        gridContainer.Rows.Add(blobItem.Attributes.Uri.AbsolutePath,size);
                    }
                }
            }
        }

        private void txtSearch_MouseClick(object sender, MouseEventArgs e)
        {

            if (txtSearch.Text.Contains("Type A Directory Or File Name to Search"))
            {
                txtSearch.Text = string.Empty;

            }
            else if(txtSearch.Text!=string.Empty)
            {
                return;
            }
            else
            {
                txtSearch.Text = string.Empty;
            }
        }       

    }
}
